<?php
class dash extends MY_Controller{

	function __construct(){
		parent::__construct();
		$this->config->load("raw");

		$this->validateAdmin();
	}
	
	function index(){
		$this->home();
	}
	
	function home(){
		$this->title = "Dashboard";
		
		/*
		 * load header
		 * @param load config header first
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("finalizeViewAdmin/header",$header,true);
		
		/*
		 * load content
		 * @note this content need width lpanel and width rpanel
		 * @param load config content first 
	
		 */
		$content = $this->config->item("content");
		$content['title'] = $this->title;
		$content['w_lpanel_main'] = "15%";
		$content['w_rpanel_main'] = "75%";
		
		$limit = 0;
		$offset = 100000000;
					
		
		$add['w_lpanel_content'] = "50%";
		$add['w_rpanel_content'] = "45%";
		$add["left"] = $this->load->view("admin/dashboard/left","",true);
		$add["right"] = $this->load->view("admin/dashboard/right","",true);
		
			$c_lpanel['admin'] = $this->load->view("admin/lpanel/admin",'',true);
			$c_lpanel['relawan'] = $this->load->view("admin/lpanel/teacher",'',true);
		$content['lpanel_main'] = $this->load->view("admin/lpanel/lpanel",$c_lpanel,true);
		$content['rpanel_main'] = $this->load->view("admin/dashboard/home",$add,true);
		$content = $this->load->view("finalizeViewAdmin/content",$content,true);
		
		/*
		 * load footer
		 * @param load config content first
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("finalizeViewAdmin/footer",$footer,true);
		
		/*
		 * finalize view 
		 * @optional : when need jscript or css ADD jscript and css with js_script() css_script() 
		 * @param config page 
		 * @finalizeView : array(title,jscript_add,css_add,jscript,css,header,content,footer);
		 */
		$jscript = js_script(array("mooEditable/MooEditable","mooEditable/MooEditable.UI.MenuList","mooEditable/MooEditable.Extras","formcheck"));
		$css = css_script(array("nav","mooEditable/MooEditable","mooEditable/MooEditable.Extras","formcheck"));
		$page = $this->config->item('page');
		$page['title'] = $this->title;
		$this->finalizeViewAdmin(array(
			'title' => $page['title'],
			'jscript_add'=>$jscript,
			'css_add'=>$css,
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header' => $header,
			'content' => $content,
			'footer' => $footer
		));
	}
	
	function category(){
		$this->title = "Category";
		
		/*
		 * @todo : load header
		 * @param : load config header first
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("finalizeViewAdmin/header",$header,true);
		
		/*
		 * load content
		 * @note this content need width lpanel and width rpanel
		 * @param load config content first 
	
		 */
		$content = $this->config->item("content");
		$content['title'] = $this->title;
		$content['w_lpanel_main'] = "15%";
		$content['w_rpanel_main'] = "75%";
		
		$limit = 0;
		$offset = 100000000;
		
		
		$add['w_lpanel_content'] = "50%";
		$add['w_rpanel_content'] = "45%";
		$add["left"] = $this->load->view("admin/dashboard/left","",true);
		$add["right"] = $this->load->view("admin/dashboard/right","",true);
		
			$c_lpanel['admin'] = $this->load->view("admin/lpanel/admin",'',true);
			$c_lpanel['relawan'] = $this->load->view("admin/lpanel/teacher",'',true);
		$content['lpanel_main'] = $this->load->view("admin/lpanel/lpanel",$c_lpanel,true);
		$content['rpanel_main'] = $this->load->view("admin/dashboard/home",$add,true);
		$content = $this->load->view("finalizeViewAdmin/content",$content,true);
		
		/*
		 * @todo : load footer
		 * @param load config content first
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("finalizeViewAdmin/footer",$footer,true);
		
		/*
		 * @todo : finalize view 
		 * @optional : when need jscript or css ADD jscript and css with js_script() css_script() 
		 * @param config page 
		 * @finalizeView : array(title,jscript_add,css_add,jscript,css,header,content,footer);
		 */
		$jscript = js_script(array("mooEditable/MooEditable","mooEditable/MooEditable.UI.MenuList","mooEditable/MooEditable.Extras","formcheck"));
		$css = css_script(array("nav","mooEditable/MooEditable","mooEditable/MooEditable.Extras","formcheck"));
		$page = $this->config->item('page');
		$page['title'] = $this->title;
		$this->finalizeViewAdmin(array(
			'title' => $page['title'],
			'jscript_add'=>$jscript,
			'css_add'=>$css,
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header' => $header,
			'content' => $content,
			'footer' => $footer
		));
	}
	
	function ajax_lesson_add(){
		$this->load->view('admin/quiz/addlesson');
	}
	
	function ajax_lesson_save(){
		$this->load->model("lessonmod");
		
		$name = $this->input->post("name");
		$id = $this->lessonmod->generateID($name);
		$id_member = get_cookie("member");
		
		$data = array(
					'id_lesson'=>$id,
					'id_member'=>$id_member,
					'name'=>$name,
				);
		
		$this->lessonmod->add($data);
		return true;
	}
	
	function ajax_subject_add(){
		$this->load->view('admin/quiz/addsubject');
	}
	
	function ajax_subject_save(){
		$this->load->model("subjectmod");
		
		$name = $this->input->post("name");
		//$id = $this->subjectmod->generateID($name);
		$id = $this->id_subject($data);
		$idl = $this->uri->segment(3,0);
		$id_member = get_cookie("member");
		$description = $this->input->post('description');
		$time = $this->input->post('time');
		$format = $this->input->post('format');
		
		$data = array(
					'id_subject'=>$id,
					'id_lesson'=>$idl,
					'author'=>$id_member,
					'name'=>$name,
					'description'=>$description,
					'time'=>$time,
					'format'=>$format,
				);
		
		$this->subjectmod->add($data);
		return true;
	}
	
	function ajax_radio_save(){
		$this->load->model("radiomod");
		
		$gen = date("H:m:s");
		$id = $this->radiomod->generateID($gen);
		$ids = $this->input->post("id_subject");
		$que = $this->input->post("question");
//		$que = str_replace(array('<p>','</p>','<p class="para">'),"",$que);
		$a = $this->input->post('a');
		$b = $this->input->post('b');
		$c = $this->input->post('c');
		$d = $this->input->post('d');
		$e = $this->input->post('e');
		$opt = $this->input->post('opt');
		
		$data = array(
					'id_radio'=>$id,
					'id_subject'=>$ids,
					'que'=>$que,
					'a'=>$a,
					'b'=>$b,
					'c'=>$c,
					'd'=>$d,
					'e'=>$e,
					'opt'=>$opt
				);
		
		$this->radiomod->add($data);
		return true;
	}
	
	function ajax_que_show(){
		$this->load->model('radiomod');
		$id = $this->uri->segment(3,0);
		
		$query = $this->lessonmod->get_where_subject($id);
		
		$this->load->view('admin/quiz/showlesson',array('lesson'=>$query));
		
	}
	
	function id_subject($data){
		$this->load->model("subjectmod");
		$id = $this->subjectmod->generateID($name);
		return $id;
	}
	
	function quiz(){
		$this->title = "Quiz";
		
		$this->load->model("subjectmod");
		
		$idl = $this->uri->segment(3,0);
		$ids = $this->uri->segment(4,0);
		$sts = $this->uri->segment(5,0);
		
		/*
		 * load header
		 * @param load config header first
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("finalizeViewAdmin/header",$header,true);
		
		/*
		 * load content
		 * @note this content need width lpanel and width rpanel
		 * @param load config content first 
	
		 */
		$content = $this->config->item("content");
		$content['title'] = $this->title;
		$content['w_lpanel_main'] = "15%";
		$content['w_rpanel_main'] = "75%";
		
		$limit = 0;
		$offset = 100000000;
		
		$subject = $this->subjectmod->get_where_id($idl);
		$que = array(
					'subject'=>$subject
				);
				
		
		
		if($idl==""){
			$add["left"] = $this->load->view("admin/quiz/left","",true);
		} else {
						
			if($ids=="" && $sts==""){
				$add["left"] = $this->load->view("admin/quiz/subject",$que,true);
			} else {
				switch($sts){
					case "add" : $add["left"] = $this->load->view("admin/quiz/subjectadd",$que,true);
						break;
					default : 
						$this->load->model('radiomod');
						$id_subject = $this->uri->segment(4,0);
						
							$q = $this->db->query("select id_subject from subject where name='$id_subject' limit 1");
							foreach ($q->result() as $re):
									$id_subject = $re->id_subject;
							endforeach;
						
						$sub = $this->radiomod->get_where_subject($id_subject);
						$ques = array('radio'=>$sub);
						
						$add["left"] = $this->load->view("admin/quiz/subjectopen",$ques,true);
						break;
				}
				
			}
		}
		
			$add['w_lpanel_content'] = "70%";
			$add['w_rpanel_content'] = "30%";
			$add["right"] = $this->load->view("admin/quiz/right","",true);
		
			$c_lpanel['admin'] = $this->load->view("admin/lpanel/admin",'',true);
			$c_lpanel['relawan'] = $this->load->view("admin/lpanel/teacher",'',true);
		$content['lpanel_main'] = $this->load->view("admin/lpanel/lpanel",$c_lpanel,true);
		$content['rpanel_main'] = $this->load->view("admin/quiz/home",$add,true);
		$content = $this->load->view("finalizeViewAdmin/content",$content,true);
		
		/*
		 * load footer
		 * @param load config content first
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("finalizeViewAdmin/footer",$footer,true);
		
		/*
		 * finalize view 
		 * @optional : when need jscript or css ADD jscript and css with js_script() css_script() 
		 * @param config page 
		 * @finalizeView : array(title,jscript_add,css_add,jscript,css,header,content,footer);
		 */
		$jscript = js_script(array("formcheck","LightFace","LightFace.Request","mooEditable/MooEditable","mooEditable/MooEditable.UI.MenuList","mooEditable/MooEditable.Extras"));
		$css = css_script(array("nav","LightFace","formcheck","mooEditable/MooEditable","mooEditable/MooEditable.Extras"));
		$page = $this->config->item('page');
		$page['title'] = $this->title;
		$this->finalizeViewAdmin(array(
			'title' => $page['title'],
			'jscript_add'=>$jscript,
			'css_add'=>$css,
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header' => $header,
			'content' => $content,
			'footer' => $footer
		));
	}
	
	function lesson_show(){
		$this->load->model('lessonmod');
		$limit = $this->uri->segment(3,0);
		$offset = $this->uri->segment(4,0);
		$search = $this->uri->segment(5,0);
		
		if($search==""){
    		$query = $this->lessonmod->get_record($limit,$offset);
		} else {
			$search =  str_replace("%20"," ",$search);
			$query = $this->lessonmod->get_where($search,$limit,$offset);
		}
		
		$this->load->view('admin/quiz/showlesson',array('lesson'=>$query));
		
	}
	
	function subject_show(){
		$this->load->model('subjectmod');
		$limit = $this->uri->segment(3,0);
		$offset = $this->uri->segment(4,0);
		$id = $this->uri->segment(5,0);
		$search = $this->uri->segment(6,0);

		$query = $this->subjectmod->get_where_id($id);
		
		$this->load->view('admin/quiz/showsubject',array('subject'=>$query));
		
	}
	
}